package cn.ekuma.epos.qkorder;

import java.awt.Component;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import com.openbravo.bean.SharedProduct;
import com.openbravo.bean.erp.Order;
import com.openbravo.bean.erp.viewbean.FindOrdersInfo;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;

import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.panels.link.I_DeleteAblePanel;

import cn.ekuma.epos.datalogic.I_DataLogicReceipts;
import cn.ekuma.epos.order.JSharedOrdersFinder;
import cn.ekuma.epos.qkorder.dialog.JEditMemoDialog;
import cn.ekuma.epos.qkorder.dialog.JOrderFilterQBFDialog;
import cn.ekuma.epos.qkorder.link.JOrderRefPanel;
import cn.ekuma.epos.qkorder.link.JProductLinkPanel;

	/**
	 *
	 * @author Administrator
	 */
	public class JLinkContainerTotalBar extends javax.swing.JPanel {

		JLinkContainerPanel container;
		private I_DataLogicReceipts dlReceipts;
		
		
	    /** Creates new form JLinkContainerTotalBar */
	    public JLinkContainerTotalBar(JLinkContainerPanel container,I_DataLogicReceipts dlReceipts) {
	        initComponents();
	        this.dlReceipts = dlReceipts;
	        this.container=container;
	        iseditable=true;
	    }

	    /** This method is called from within the constructor to
	     * initialize the form.
	     * WARNING: Do NOT modify this code. The content of this method is
	     * always regenerated by the Form Editor.
	     */
	    @SuppressWarnings("unchecked")
	    // <editor-fold defaultstate="collapsed" desc="Generated Code">
	    private void initComponents() {

	        jAddProductLinkPanel = new javax.swing.JButton();
	        jRemoveProductLinkPanel = new javax.swing.JButton();
	        jHide = new javax.swing.JButton();
	        jEditMemo = new javax.swing.JButton();
	        jSeparator1 = new javax.swing.JSeparator();
	        jrefulshCoult = new javax.swing.JButton();
	        jImportCoult = new javax.swing.JButton();
	        jButton1 = new javax.swing.JButton();
	        jOrderRef = new javax.swing.JButton();
	        jFilterLabel = new javax.swing.JLabel();

	        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));

	        jAddProductLinkPanel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/editnew.png"))); // NOI18N
	        jAddProductLinkPanel.setFocusable(false);
	        jAddProductLinkPanel.setPreferredSize(new java.awt.Dimension(40, 25));
	        jAddProductLinkPanel.setRequestFocusEnabled(false);
	        jAddProductLinkPanel.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jAddProductLinkPanelActionPerformed(evt);
	            }
	        });
	        add(jAddProductLinkPanel);

	        jRemoveProductLinkPanel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/editdelete.png"))); // NOI18N
	        jRemoveProductLinkPanel.setFocusable(false);
	        jRemoveProductLinkPanel.setPreferredSize(new java.awt.Dimension(40, 25));
	        jRemoveProductLinkPanel.setRequestFocusEnabled(false);
	        jRemoveProductLinkPanel.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jRemoveProductLinkPanelActionPerformed(evt);
	            }
	        });
	        add(jRemoveProductLinkPanel);

	        jHide.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/1downarrow.png"))); // NOI18N
	        jHide.setFocusable(false);
	        jHide.setPreferredSize(new java.awt.Dimension(40, 25));
	        jHide.setRequestFocusEnabled(false);
	        jHide.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jHideActionPerformed(evt);
	            }
	        });
	        add(jHide);

	        jEditMemo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/edit.png"))); // NOI18N
	        jEditMemo.setFocusable(false);
	        jEditMemo.setPreferredSize(new java.awt.Dimension(40, 25));
	        jEditMemo.setRequestFocusEnabled(false);
	        jEditMemo.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jEditMemoActionPerformed(evt);
	            }
	        });
	        add(jEditMemo);

	        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
	        jSeparator1.setMaximumSize(new java.awt.Dimension(10, 32767));
	        jSeparator1.setPreferredSize(new java.awt.Dimension(2, 10));
	        jSeparator1.setRequestFocusEnabled(false);
	        jSeparator1.setVerifyInputWhenFocusTarget(false);
	        add(jSeparator1);

	        jrefulshCoult.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/reload.png"))); // NOI18N
	        jrefulshCoult.setFocusable(false);
	        jrefulshCoult.setPreferredSize(new java.awt.Dimension(40, 25));
	        jrefulshCoult.setRequestFocusEnabled(false);
	        jrefulshCoult.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jrefulshCoultActionPerformed(evt);
	            }
	        });
	        add(jrefulshCoult);

	        jImportCoult.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/inbox.png"))); // NOI18N
	        jImportCoult.setFocusable(false);
	        jImportCoult.setPreferredSize(new java.awt.Dimension(40, 25));
	        jImportCoult.setRequestFocusEnabled(false);
	        jImportCoult.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jImportCoultActionPerformed(evt);
	            }
	        });
	        add(jImportCoult);

	        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/ark216.png"))); // NOI18N
	        jButton1.setFocusable(false);
	        jButton1.setFocusable(false);
	        jButton1.setPreferredSize(new java.awt.Dimension(40, 25));
	        jButton1.setRequestFocusEnabled(false);
	        jButton1.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jButton1ActionPerformed(evt);
	            }
	        });
	        add(jButton1);

	        jOrderRef.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/clipart.png"))); // NOI18N
	        jOrderRef.setFocusable(false);
	        jOrderRef.setPreferredSize(new java.awt.Dimension(40, 25));
	        jOrderRef.setRequestFocusEnabled(false);
	        jOrderRef.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jOrderRefActionPerformed(evt);
	            }
	        });
	        add(jOrderRef);

	        jFilterLabel.setFocusable(false);
	        jFilterLabel.setMaximumSize(new java.awt.Dimension(400, 25));
	        jFilterLabel.setMinimumSize(new java.awt.Dimension(100, 25));
	        jFilterLabel.setPreferredSize(new java.awt.Dimension(200, 25));
	        jFilterLabel.setRequestFocusEnabled(false);
	        add(jFilterLabel);
	    }// </editor-fold>

	    private void jAddProductLinkPanelActionPerformed(java.awt.event.ActionEvent evt) {
	        try {
	            JOrderFilterQBFDialog orderTypeFiler = JOrderFilterQBFDialog.getOrderFilterQBFDialog((Component)this,container.getApp(), container.getDlSales(), container.getDlCustomers());
	            orderTypeFiler.setVisible(true);
	            if (orderTypeFiler.isOK()) {
	                JProductLinkPanel tabPanel=new JProductLinkPanel(container.getDlSales(),orderTypeFiler.createValue());
	                container.addPanel("title", tabPanel);
	            }
	        } catch (BasicException ex) {
	        }
	}

	    private void jRemoveProductLinkPanelActionPerformed(java.awt.event.ActionEvent evt) {
	        Component selectTab=container.getSelectedComponent();
	        if(selectTab!=null&&selectTab instanceof I_DeleteAblePanel&&((I_DeleteAblePanel)selectTab).deactivate())
	        	container.removePanel(selectTab);
	}

	    private void jHideActionPerformed(java.awt.event.ActionEvent evt) {
	        if(container.hide){
	        	container.hide=false;
	        	container.setVisible(true);
	            jHide.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/1downarrow.png")));
	            jAddProductLinkPanel.setEnabled(true);
	            jRemoveProductLinkPanel.setEnabled(true);
	        }else{
	        	container.hide=true;
	        	container.setVisible(false);
	            jHide.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/1uparrow.png")));
	            jAddProductLinkPanel.setEnabled(false);
	            jRemoveProductLinkPanel.setEnabled(false);
	        }
	        container.printPartialTotals();
	}

	    private void jEditMemoActionPerformed(java.awt.event.ActionEvent evt) {
	        if(!iseditable)
	        	return;
	    	JEditMemoDialog memoEdit=JEditMemoDialog.getDialog(this, container.getOrderManager().getOrder().getMemo());
	        memoEdit.setVisible(true);
	        if(memoEdit.isOk()){
	        	container.getOrderManager().getOrder().setMemo(memoEdit.getRetStr());
	            
	        }
	}

	    private void jrefulshCoultActionPerformed(java.awt.event.ActionEvent evt) {
	    	if(!iseditable)
	        	return;
	    	int res = JOptionPane.showConfirmDialog(this, AppLocal.getIntString("message.needmoretime"), AppLocal.getIntString("title.editor"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
	        if (res == JOptionPane.YES_OPTION)
	        	container.getjPanelOrder().refulshCoult();
	}

	    private void jImportCoultActionPerformed(java.awt.event.ActionEvent evt) {
	    	if(!iseditable)
	        	return;
	    	int res = JOptionPane.showConfirmDialog(this, AppLocal.getIntString("message.needmoretime"), AppLocal.getIntString("title.editor"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
	        if (res == JOptionPane.YES_OPTION)
	        	container.getjPanelOrder().importConsult();
	}

	    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
	        if(container.getProductID()!=null){
	            try {
	            	container.getDlSales().saveSharedProduct(new SharedProduct(container.getProductID()));
	            } catch (BasicException ex) {
	                new MessageInf(ex).show(this);
	            }
	        }
	}

	    private void jOrderRefActionPerformed(java.awt.event.ActionEvent evt) {
	    	SwingUtilities.invokeLater(new Runnable() {
	            public void run() {             
	                try {
	                    JSharedOrdersFinder orderFinder=JSharedOrdersFinder.getOrderFinder(container, container.app, dlReceipts, container.getDlSales(), container.getDlCustomers());
	                    orderFinder.setVisible(true);
	                    FindOrdersInfo findOrder=orderFinder.getSelectedOrder();
	                    if (findOrder != null) {
	                    	Order order = dlReceipts.getSharedOrder(findOrder.id);
	                        if (order == null)  {
	                            throw new BasicException(AppLocal.getIntString("message.noOrder"));
	                        } else{ 
	                            //dlReceipts.setSharedOrderLocked(findOrder.id,true); 
	                            JOrderRefPanel orderRef=new JOrderRefPanel(container,order,true, container.getjPanelOrder().getDlSystem());
	                            container.addPanel("ref", orderRef);
	                            orderRef.addProductChangedListener(container.getjPanelOrder());
	                        }
	                    }
	                } catch (BasicException e) {
	                    new MessageInf(e).show(container); 
	                }                    
	            }
	        }); 
	    }


	    // Variables declaration - do not modify
	    private javax.swing.JButton jAddProductLinkPanel;
	    private javax.swing.JButton jButton1;
	    private javax.swing.JButton jEditMemo;
	    private javax.swing.JLabel jFilterLabel;
	    private javax.swing.JButton jHide;
	    private javax.swing.JButton jImportCoult;
	    private javax.swing.JButton jOrderRef;
	    private javax.swing.JButton jRemoveProductLinkPanel;
	    private javax.swing.JSeparator jSeparator1;
	    private javax.swing.JButton jrefulshCoult;
		private boolean iseditable;
	    // End of variables declaration


		public void resetComponent() {
			iseditable=container.getOrderManager().isEditAble();
			jEditMemo.setEnabled(iseditable);
			jImportCoult.setEnabled(iseditable);
            jrefulshCoult.setEnabled(iseditable);
		    if(!container.getOrderManager().getOrder().getDefineType().isConsult()){
		        jImportCoult.setVisible(false);
		        jrefulshCoult.setVisible(false);
		     }
		}

		public void printParialTotals() {
			String name=AppLocal.getIntString("label.articlescount")+":\t"+container.getOrderManager().printArticlesCount()+"\t"+AppLocal.getIntString("label.totalcash")+":\t"+container.getOrderManager().printTotal();
            jFilterLabel.setText(name);
		}

}
